package cn.stylefeng.guns.modular.hx.storage.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.agv.model.params.FeedbackTaskRS;
import cn.stylefeng.guns.modular.agv.util.TustApiUtils;
import cn.stylefeng.guns.modular.hx.storage.entity.HxCollectionLocation;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxCollectionLocationParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxCollectionLocationResult;
import cn.stylefeng.guns.modular.hx.storage.service.HxCollectionLocationService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 控制器
 *
 * @author lww
 * @Date 2024-01-02 11:03:40
 */
@Controller
@RequestMapping("/hxCollectionLocation")
public class HxCollectionLocationController extends BaseController {

    private String PREFIX = "/hx/storage/hxCollectionLocation";

    @Value("${tusk.baseUrl}")
    private String tuskBaseUrl;

    @Autowired
    private HxCollectionLocationService hxCollectionLocationService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author lww
     * @Date 2024-01-02
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/hxCollectionLocation.html";
    }

    /**
     * 新增页面
     *
     * @author lww
     * @Date 2024-01-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/hxCollectionLocation_add.html";
    }

    /**
     * 编辑页面
     *
     * @author lww
     * @Date 2024-01-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/hxCollectionLocation_edit.html";
    }

    /**
     * 新增接口
     *
     * @author lww
     * @Date 2024-01-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(HxCollectionLocationParam hxCollectionLocationParam) {
        this.hxCollectionLocationService.add(hxCollectionLocationParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author lww
     * @Date 2024-01-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(HxCollectionLocationParam hxCollectionLocationParam) {

        this.hxCollectionLocationService.update(hxCollectionLocationParam);
        return ResponseData.success();
    }
    @RequestMapping("/clear")
    @ResponseBody
    public ResponseData clear(String stockPort ) {
        User user = userService.getcurrentUser();
        HxCollectionLocationParam hxCollectionLocationParam=new HxCollectionLocationParam();
        hxCollectionLocationParam.setGetUnit("");
        hxCollectionLocationParam.setGetUnitId(null);
        hxCollectionLocationParam.setStockPort(stockPort);
        hxCollectionLocationParam.setStatus(null);
        hxCollectionLocationParam.setTrayId(null);
        hxCollectionLocationParam.setTrayNumber(null);

        hxCollectionLocationParam.setDeptId(user.getDeptId());

        hxCollectionLocationParam.setOperateId(user.getOperateId());
        this.hxCollectionLocationService.clear(hxCollectionLocationParam);
        //清空对应的巷道
        FeedbackTaskRS res = TustApiUtils.clear(tuskBaseUrl, "H"+ StringUtils.leftPad(stockPort,3,"0"));
        return ResponseData.success();
    }
    /**
     * 删除接口
     *
     * @author lww
     * @Date 2024-01-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(HxCollectionLocationParam hxCollectionLocationParam) {
        this.hxCollectionLocationService.delete(hxCollectionLocationParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author lww
     * @Date 2024-01-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(HxCollectionLocationParam hxCollectionLocationParam) {
        HxCollectionLocation detail = this.hxCollectionLocationService.getById(hxCollectionLocationParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author lww
     * @Date 2024-01-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(HxCollectionLocationParam hxCollectionLocationParam,String condition ) {

        User user = userService.getcurrentUser();
        hxCollectionLocationParam.setDeptId(user.getDeptId());
        hxCollectionLocationParam.setDeliverId(user.getDeliverId());
        return this.hxCollectionLocationService.findPageBySpec(hxCollectionLocationParam,condition);
    }
    @ResponseBody
    @RequestMapping("/find")
    public List<HxCollectionLocationResult>  find(HxCollectionLocationParam hxCollectionLocationParam,String condition) {
        User user = userService.getcurrentUser();
        hxCollectionLocationParam.setDeptId(user.getDeptId());
        hxCollectionLocationParam.setOperateId(user.getOperateId());
        List <HxCollectionLocationResult> pageBySpec = this.hxCollectionLocationService.find(hxCollectionLocationParam, condition);
        return pageBySpec;
    }




    @ResponseBody
    @RequestMapping("/cha")
    public LayuiPageInfo cha(HxCollectionLocationParam hxCollectionLocationParam,String condition ,String stockPort) {
        User user = userService.getcurrentUser();
        hxCollectionLocationParam.setDeptId(user.getDeptId());
        hxCollectionLocationParam.setOperateId(user.getOperateId());
        hxCollectionLocationParam.setStockPort(stockPort);
        return this.hxCollectionLocationService.findPageBySpec(hxCollectionLocationParam,condition);
    }
    @ResponseBody
    @RequestMapping("/findLocation")
    public List<HxCollectionLocationResult>  findLocation(String stockPort) {
        HxCollectionLocationParam hxCollectionLocationParam=new HxCollectionLocationParam();
        hxCollectionLocationParam.setStockPort(stockPort);
        User user = userService.getcurrentUser();
        hxCollectionLocationParam.setDeptId(user.getDeptId());
        hxCollectionLocationParam.setOperateId(user.getOperateId());
        List<HxCollectionLocationResult> location = this.hxCollectionLocationService.findLocation(hxCollectionLocationParam);
        return location;
    }

    /**
     * 根据门店获取所有的通道
     * @param getUnitId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getStockPortByUnit")
    public List<HxCollectionLocationResult>  getStockPortByUnit(String getUnitId) {
        HxCollectionLocationParam hxCollectionLocationParam=new HxCollectionLocationParam();
        hxCollectionLocationParam.setGetUnitId(getUnitId);
        User user = userService.getcurrentUser();
        hxCollectionLocationParam.setDeptId(user.getDeptId());
        hxCollectionLocationParam.setOperateId(user.getOperateId());
        List<HxCollectionLocationResult> location = this.hxCollectionLocationService.getStockPortByUnit(hxCollectionLocationParam);
        return location;
    }
}


